//
//  THCPCHallVC.m
//  YJB
//
//  Created by YJB on 2016/11/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THCPCHallVC.h"
#import "THCpcHallCell.h"
#import "masonry.h"
#import "YJUserDefaultTool.h"
#import "YJNetworkTool.h"
#import "THCpcHallModel.h"
#import "THTaskReportVC.h"
#import "THDoTaskVC.h"
#import "THCpcTaskPublishVC.h"
#import "YJGlobalHeader.h"
#import "THCPCPublishNewVC.h"

@interface THCPCHallVC ()
//@property(nonatomic,strong)UIView *headView;
//@property(nonatomic,strong)NSMutableArray *tasks;
@end

@implementation THCPCHallVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    self.tableView.backgroundColor = kUIColorFromRGB(0xeeeeee);
//    self.tableView.rowHeight = 50;
//    //self.tableView.backgroundColor = kUIColorFromRGB(0xffebe7);
//    self.tableView.tableFooterView = [UIView new];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    [self.tableView registerClass:[THCpcHallCell class] forCellReuseIdentifier:cpcHallCellID];
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
//    [self.tableView.mj_header beginRefreshing];
    //self.tableView.tableHeaderView = self.headView;
    
    @weakify(self);
    [[[NOTIFICATIONCENTER rac_addObserverForName:THUserDidDeleteCpcTaskNotification object:nil] takeUntil:self.rac_willDeallocSignal]subscribeNext:^(NSNotification *x) {
        @strongify(self);
        NSString *ID = x.userInfo[@"taskId"];
        THCpcHallModel *selectTask = nil;
        for (THCpcHallModel *task in self.tasks)
        {
            if ([task.ID isEqualToString:ID])
            {
                selectTask = task;
                break;
            }
        }
        [self.tasks removeObject:selectTask];
        [self.tableView reloadData];
    }];
    
    YJWeakSelf
    [[[NOTIFICATIONCENTER rac_addObserverForName:THUserDidPublishCpcTaskNotification object:nil] takeUntil:self.rac_willDeallocSignal]subscribeNext:^(id x) {
        [weakSelf loadData];
    }];
    
}


//- (UIView *)headView
//{
//    if (_headView == nil)
//    {
//        _headView = [[UIView alloc] init];
//        _headView.backgroundColor = WHITECOLOR;
//        _headView.height = 85;
//        UILabel *label = [[UILabel alloc] init];
//        [_headView addSubview:label];
//        label.textColor = kUIColorFromRGB(0x999999);
//        label.font = FONT(14);
//        label.text = @"点击按钮发布新任务";
//        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(12);
//            make.height.mas_equalTo(20);
//            make.width.mas_equalTo(200);
//            make.top.mas_equalTo(15);
//        }];
//        
//        UILabel *tip = [[UILabel alloc]init];
//        tip.text = @"官方提示:";
//        tip.textAlignment = NSTextAlignmentCenter;
//        tip.backgroundColor = kUIColorFromRGB(0xf4f3f3);
//        tip.font = FONT(14);
//        tip.textColor = kUIColorFromRGB(0xd01e06);
//        [_headView addSubview:tip];
//        [tip mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.mas_equalTo(0);
//            make.top.mas_equalTo(label.mas_bottom).offset(10);
//            make.height.mas_equalTo(18);
//            make.width.mas_equalTo(86);
//        }];
//        
//        UILabel *tipTwo = [[UILabel alloc]init];
//        tipTwo.text = @"        如要求出钱或发短信才能完成的任务,请不要做,谢谢!";
//        tipTwo.font = FONT(12);
//        tipTwo.backgroundColor = kUIColorFromRGB(0xf4f3f3);
//        tipTwo.textColor = kUIColorFromRGB(0x222222);
//        [_headView addSubview:tipTwo];
//        [tipTwo mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.bottom.right.mas_equalTo(0);
//            make.top.mas_equalTo(tip.mas_bottom);
//        }];
//        
//        UIButton *btn = [[UIButton alloc] init];
//        [btn setImage:Image(@"添加按钮") forState:UIControlStateNormal];
//        btn.titleLabel.font = FONT(16);
//        [_headView addSubview:btn];
//        [btn addTarget:self action:@selector(addNewTask) forControlEvents:UIControlEventTouchUpInside];
//        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(-20);
//            make.height.height.mas_equalTo(50);
//            make.centerY.mas_equalTo(_headView.mas_centerY);
//        }];
//    }
//    return _headView;
//}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.tasks.count;
    //return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THCpcHallCell *cell = [tableView dequeueReusableCellWithIdentifier:cpcHallCellID forIndexPath:indexPath];
    cell.model = self.tasks[indexPath.section];
    return cell;
}

- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    NSString *userName = [YJUserDefaultTool readUserName];
    NSString *md5KeyId = [YJUserDefaultTool readKeyID];
    params[@"action"] = @"get_all_list";
    params[@"makegold"] = @{@"username":userName,
                            @"firstrow":@"-1",
                            @"listrows":@"",
                            @"user_info":userName,
                            @"phone_info":@"iOS",
                            @"md5_key":md5KeyId};
    NSString *url = THURL(@"cpcclick.php");
    
    [YJNetworkTool postWithUrl:url parameters:params success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        NSString *code = responseObject[@"return"][@"code"];
        if (code.integerValue == 200)
        {
            NSArray *array = responseObject[@"return"][@"data"][@"list"];
        self.tasks = [THCpcHallModel mj_objectArrayWithKeyValuesArray:array];
        }
        else if(code.integerValue == 500)
            [self.tasks removeAllObjects];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络不佳，稍后重试" toView:self.view];
    }];
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return self.tasks.count;
//}
//
//#pragma mark ----------------------- 表格代理方法
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    return 1;
//    //return _tasks.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    THCpcHallCell *cell = [tableView dequeueReusableCellWithIdentifier:cpcHallCellID forIndexPath:indexPath];
//    cell.model = _tasks[indexPath.section];
//    return cell;
//}



- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    THCpcHallModel *model = self.tasks[indexPath.row];
    YJWeakSelf
    UITableViewRowAction *taskAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"做任务" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        THDoTaskVC *taskVC = [[THDoTaskVC alloc]init];
        taskVC.model = model;
        [weakSelf.navigationController pushViewController:taskVC animated:YES];
       [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    taskAction.backgroundColor = kUIColorFromRGB(0xfa5f3c);
    UITableViewRowAction *reportAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"举报" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        THTaskReportVC *reportVC = [[THTaskReportVC alloc]init];
        reportVC.taskTitle = model.title;
        reportVC.ID = model.ID;
        reportVC.taskType = @"3";
        [weakSelf.navigationController pushViewController:reportVC animated:YES];
        [weakSelf.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    }];
    reportAction.backgroundColor = kUIColorFromRGB(0xf5a741);
    return @[taskAction,reportAction];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    THCpcHallModel *model = _tasks[indexPath.row];
//    THDoTaskVC *taskVC = [[THDoTaskVC alloc]init];
//    taskVC.model = model;
//    [self.navigationController pushViewController:taskVC animated:YES];
}

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *headerView = [[UIView alloc]init];
//    headerView.backgroundColor = kUIColorFromRGB(0xffebe7);
//    
//    UIView *lineOne = [[UIView alloc]init];
//    lineOne.backgroundColor = kUIColorFromRGB(0xcccccc);
//    [headerView addSubview:lineOne];
//    [lineOne mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.mas_equalTo(0);
//        make.height.mas_equalTo(0.5);
//    }];
//    
//    UIView *lineTwo = [[UIView alloc]init];
//    lineTwo.backgroundColor = kUIColorFromRGB(0xcccccc);
//    [headerView addSubview:lineTwo];
//    [lineTwo mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.mas_equalTo(0);
//        make.height.mas_equalTo(0.5);
//    }];
//    
//    NSArray *titles = @[@"任务标题",@"普通/VIP奖励",@"剩余名额"];
//    NSArray *widths = @[@(IPHONE_W - 18 * GRIDW),@(9 * GRIDW),@( 9 * GRIDW)];
//    UILabel *lastLab = nil;
//    for (int i = 0; i < titles.count; i ++)
//    {
//        UILabel *lab = [[UILabel alloc]init];
//        lab.textColor = kUIColorFromRGB(0x333333);
//        lab.font = FONT(15);
//        lab.text = titles[i];
//        lab.textAlignment = NSTextAlignmentCenter;
//        [headerView addSubview:lab];
//        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.mas_equalTo(0.5);
//            make.bottom.mas_equalTo(-0.5);
//            make.width.equalTo(widths[i]);
//            if (lastLab)
//                make.left.mas_equalTo(lastLab.mas_right);
//            else
//                make.left.mas_equalTo(0);
//        }];
//        lastLab = lab;
//    }
//    return headerView;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 80;
//}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return [self.tableView fd_heightForCellWithIdentifier:cpcHallCellID cacheByIndexPath:indexPath configuration:^(THCpcHallCell *cell) {
//        
//        cell.model = self.tasks[indexPath.section];
//    }];
//}



//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (0 == section) {
//        return 10;
//    }
//    return 5;
//}


- (void)addNewTask
{
    THCpcTaskPublishVC *taskVC = [[THCpcTaskPublishVC alloc]init];
    [self.navigationController pushViewController:taskVC animated:YES];
//
//    THCPCPublishNewVC *pub = [[THCPCPublishNewVC alloc] init];
//    [self.navigationController pushViewController:pub animated:YES];
}

@end
